package com.samsung;

import java.util.*;
import java.lang.String;

public class BackWater {

    //状态内部类，
    static class State{
        int[] s;
        int step;
        public State(int x, int y, int z){
            s = new int[]{x, y, z};
            step = 0;
        }
    }
    //最终状态，减少方法参数。
    static State endState;

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(); // n组测试数据。
        while (n-- > 0){

            State initState = new State(in.nextInt(),in.nextInt(),in.nextInt());

            endState = new State(in.nextInt(), in.nextInt(), in.nextInt());

            int time = BFS(initState);

            System.out.println(time);
        }
    }

    //计算 状态改变为最终状态的步数，如果不行，返回-1；
    private static int BFS(State s) {
        State initial = new State(s.s[0],0,0);

        Queue<State> que = new LinkedList<>();
        que.offer(initial);

        Set<String> visited = new HashSet<>();
        visited.add(Arrays.toString(initial.s));

        while (!que.isEmpty()){
            State curState = que.poll();

            if(Arrays.equals(curState.s,endState.s)){
                return curState.step;
            }

            //尝试所有倒水组合
            for (int i = 0; i < 3; i++) {
                for (int j = 0; j < 3; j++) {
                    if(i != j){
                        pourWater(curState,s, i,j);
                        curState.step++;
                        String newStateString = Arrays.toString(curState.s);

                        if(!visited.contains(newStateString)){
                            que.offer(curState);
                            visited.add(newStateString);
                        }
                    }
                }
            }
        }
        return -1;
    }

    private static void pourWater(State curState, State capacity, int from, int to) {
        int waterNeed = capacity.s[to] - curState.s[to];

        if(waterNeed <= curState.s[from]){
            curState.s[from] = curState.s[from] - waterNeed;
            curState.s[to] = curState.s[to] + waterNeed;
        }else {
            curState.s[to] = curState.s[from];
            curState.s[from] = 0;
        }
    }


}
